#include<stdbool.h>
#include<assert.h>
#include<string.h>
#include <ctype.h> 

struct UsertPartData
{
	char birth_address[7]; // 前6位
	char yead[5]; 
	char month[3];
	char day[3];
	char IDcode[5];
};


bool isIdcard(char* id) // 身份证格式验证
{
	int i = 0;
	int len = strlen(id);
	if(len != 18)
		return false;
	char* cur = id;
	for (i = 0; i < 17; i++)
	{
		if (!isdigit(cur[i]))
			return false;

	}
	if (!(isdigit(cur[i]) || cur[i] == 'X' || cur[i] == 'x'))
		return false;
	return true;
}

void IdDecide(char* id, struct UsertPartData* ps)  // 划分身份
{
	assert(ps && id);
	
	// assert(isIdcard(id));
	if (!isIdcard(id))
	{
		printf("非身份证格式!!!\n");
		exit(-1);
	}
	
	int i = 0;
	int j = 0;
	for (j = 0; j < 6; j++)
	{	
		ps->birth_address[j] = id[i];
		i++;
	}
	ps->birth_address[j] = '\0';

	for (j = 0; j < 4; j++)
	{
		ps->yead[j] = id[i];
		i++;
	}
	ps->yead[j] = '\0';

	for (j = 0; j < 2; j++)
	{
		ps->month[j] = id[i];
		i++;
	}
	ps->month[j] = '\0';

	for (j = 0; j < 2; j++)
	{
		ps->day[j] = id[i];
		i++;
	}
	ps->day[j] = '\0';
	
	for (j = 0; j < 4; j++)
	{
		ps->IDcode[j] = id[i];
		i++;
	}
	ps->IDcode[j] = '\0';
	return;
}

bool IdIsBoy(struct UsertPartData* ps)  
{
	assert(ps);

	int len = strlen(ps->IDcode);
	int sex_num = len - 2;
	if (ps->IDcode[sex_num] % 2 == 0)
		return false;
	return true;
}

void IdPrint(struct UsertPartData* ps)
{
	assert(ps);
	printf("划分后： %s  %s  %s  %s  %s ", ps->birth_address,ps->yead,ps->month,ps->day,ps->IDcode);
}

void TestIdCard()
{
	struct UsertPartData U;
	char IdcardNumber[19] = { 0 };
	printf("Input your IdCard : ");
	scanf("%s", IdcardNumber);
	// 360721 \0 2003 \0 11 \0 24 \0 7253
	IdDecide(IdcardNumber, &U);

	printf("身份证号：%s\n", IdcardNumber);
	// 判断性别
	int sex = IdIsBoy(&U);
	if (sex == true)
		printf("sex: male\n");
	else
		printf("sex: female\n");

	IdPrint(&U);
}
int main()
{
	TestIdCard();
	return 0;
}